<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>规格管理</title>
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="../css/style.css">
	<script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="../plugins/bootstrap/js/bootstrap.min.js"></script>

	<script type="text/javascript">

        //跳转页面
        function goPage(pageIndex,pageSize) {
            //contentCategory_page?pageIndex=1&pageSize=${pageUtils.pageSize}
            //搜索表单,动态修改action属性
            $("#searchForm").prop("action","specification_page?pageIndex="+pageIndex+"&pageSize="+pageSize);
            //当表单提交，那么表单中的参数也会一起提交
            $("#searchForm").submit();
        }
        
        //新增规格
		function addSpecification() {
            var data=$("#addForm").serialize();
            $.ajax({
                url:"specification_add",
                data:data,
                type:"post",
                dataType:"json",
                success:function (result) {
//                    alert(JSON.stringify(result));
                    if(result.code==200){
                        alert(result.message);
                        location.href="specification_page";
                    }else if(result.code==503){
                        alert(result.message);
					}else{
                        alert(result.message);
                    }
                }
            });
        }
        //新增规格选项
        function addOptions_add() {
            $("#tbody_options_add").append("<tr><td><input  class=\"form-control\" name=\"option_name\" placeholder=\"规格选项\"> </td><td><input  class=\"form-control\" name=\"orders\" placeholder=\"排序\"> </td><td><button type=\"button\" class=\"btn btn-default\" title=\"删除\" onclick=\"deleteOption_add(this);\"><i class=\"fa fa-trash-o\"></i> 删除</button></td></tr>");
        }
        //修改规格选项
		function addOptions_update() {
            $("#tbody_options_update").append("<tr><td><input  class=\"form-control\" name=\"option_name\" placeholder=\"规格选项\"> </td><td><input  class=\"form-control\" name=\"orders\" placeholder=\"排序\"> </td><td><button type=\"button\" class=\"btn btn-default\" title=\"删除\" onclick=\"deleteOption_update(this);\"><i class=\"fa fa-trash-o\"></i> 删除</button></td></tr>");

        }
		//新增删除规格
		function deleteOption_add(btn) {
            if($("#tbody_options_add").find("tr").length>1){
                $(btn).parent().parent().remove();
			}else{
                alert("不能再删除了，至少留一个啊");
			}
        }
        //修改删除规格
		function deleteOption_update(btn) {
			if($("#tbody_options_update").find("tr").length>1){
			    $(btn).parent().parent().remove();
			}else{
			    alert("最少要有一个哦，不可以搞黄色哦");
			}
        }
        //修改的回显
		function goUpdate(id) {
			$.getJSON("specification_getById",{"id":id},function (result) {
			    $("#id_update").val(result.specification.id);
			    $("#spec_name_update").val(result.specification.spec_name);
                var options=result.specificationOptions;
                //置空
                $("#tbody_options_update").text("");
                for(var i=0;i<options.length;i++){
                    var option=options[i];
                    $("#tbody_options_update").append("<tr><td><input  class=\"form-control\" value='"+option.option_name+"' name=\"option_name\" placeholder=\"规格选项\"></td><td><input  class=\"form-control\" name=\"orders\" value='"+option.orders+"' placeholder=\"排序\"></td><td><button type=\"button\" class=\"btn btn-default\" title=\"删除\" onclick=\"deleteOption_update(this);\"><i class=\"fa fa-trash-o\"></i> 删除</button></td></tr>");
                }
            });
        }
        
        //更新规格
		function updateSpecification() {
			var data = $("#updateForm").serialize();
            $.ajax({
                url:"specification_update",
                data:data,
                type:"post",
                dataType:"json",
                success:function (result) {
                    alert(JSON.stringify(result));
                    if(result.code==200){
                        alert(result.message);
                        location.href="specification_page";
                    }else if(result.code==503){
                        alert("服务器异常......");
                    }else{
                        alert(result.message);
                    }
                }
            });
        }
	</script>
</head>

<body class="hold-transition skin-red sidebar-mini" >
  <!-- .box-body -->                
                    <div class="box-header with-border">
                        <h3 class="box-title">规格管理</h3>
                    </div>
                    <div class="box-body">
                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建"  data-toggle="modal" data-target="#addModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" title="删除" ><i class="fa fa-trash-o"></i> 删除</button>
                                        
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                                    </div>
                                </div>
                            </div>
							<form id="searchForm" action="specification_page?pageIndex=1&pageSize=5" method="post">
                            <div class="box-tools pull-right">
                                <div class="has-feedback">
							                    规格名称：<input name="spec_name" th:value="${spec_name}" >
									<button class="btn btn-default" type="submit">查询</button>
                                </div>
                            </div>
							</form>
                            <!--工具栏/-->
			                  <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input id="selall" type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">规格ID</th>
									      <th class="sorting">规格名称</th>									     												
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody>
			                          <tr th:each="specification:${pageUtils.records}">
			                              <td><input  type="checkbox" ></td>			                              
				                          <td th:text="${specification.id}">1</td>
									      <td th:text="${specification.spec_name}">网络制式</td>
		                                  <td class="text-center">                                           
		                                 	  <button type="button" class="btn bg-olive btn-xs" data-toggle="modal" th:onclick="|goUpdate(${specification.id})|" data-target="#editModal">修改</button>
		                                  </td>
			                          </tr>
			                      </tbody>
			                  </table>
			                  <!--数据列表/-->
							<!-- 分页组件 -->
							<div class="row">
								<div class="col-sm-6"><div class="dataTables_paginate paging_bootstrap" id="sample-table_paginate">
									<ul class="pagination">
										<li class="prev"><a th:href="|javascript:goPage(1,${pageUtils.pageSize});|">首页</a></li>
										<!--<li class="prev" th:if="${pageUtils.pageIndex>1}"><a th:href="|brand_page?pageIndex=${pageUtils.pageIndex-1}&pageSize=${pageUtils.pageSize}|">上一页</a></li>-->

										<li class="next" th:each="num:${pageUtils.numbers}" th:class="${pageUtils.pageIndex==num}? active"><a th:href="|javascript:goPage(${num},${pageUtils.pageSize});|" th:text="${num}">XXX</a></li>

										<!--	<li class="next" th:if="${pageUtils.pageIndex<pageUtils.pageCount}"><a th:href="|brand_page?pageIndex=${pageUtils.pageIndex+1}&pageSize=${pageUtils.pageSize}|">下一页</a></li>-->
										<li class="next"><a th:href="|javascript:goPage(${pageUtils.pageCount},${pageUtils.pageSize});|">尾页</a></li>
										<li class="next"><a href="#" th:text="|当前${pageUtils.pageIndex}/${pageUtils.pageCount}页|"></a></li>
										<li class="next"><a href="#" th:text="|共${pageUtils.totalCount}条|">XXX</a></li>
									</ul>
								</div>
								</div>
							</div>
							<!-- 分页组件 -->
                        </div>
                        <!-- 数据表格 /--> 
                     </div>
                    <!-- /.box-body -->                    
	          		    
                                
<!-- 编辑窗口 -->
  <form id="updateForm" method="post" action="#">
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">规格编辑</h3>
		</div>
		<div class="modal-body">							
			
			<table class="table table-bordered table-striped"  width="800px">
		      	<tr>
		      		<td>规格名称</td>
		      		<td>
						<!--隐藏需要更新的id值，不然拿不到值-->
						<input type="hidden" name="id" id="id_update">
						<input  class="form-control" name="spec_name" id="spec_name_update" placeholder="规格名称" >
					</td>
		      	</tr>
			 </table>				
			 
			 <!-- 规格选项 -->
			 <div class="btn-group">
                  <button type="button" class="btn btn-default" onclick="addOptions_update();" title="新建" ><i class="fa fa-file-o"></i> 新增规格选项</button>
                  
             </div>
			 
			 <table class="table table-bordered table-striped table-hover dataTable">
                    <thead>
                        <tr>
				      <th class="sorting">规格选项</th>
				      <th class="sorting">排序</th>																
                      <th class="sorting">操作</th>	
                    </thead>
                    <tbody id="tbody_options_update">
                      <!--<tr>-->
                           <!---->
				            <!--<td>-->
				            	<!--<input  class="form-control" placeholder="规格选项"> -->
				            <!--</td>-->
				            <!--<td>-->
				            	<!--<input  class="form-control" placeholder="排序"> -->
				            <!--</td>-->
							<!--<td>-->
								<!--<button type="button" class="btn btn-default" title="删除" ><i class="fa fa-trash-o"></i> 删除</button>-->
							<!--</td>-->
                      <!--</tr>-->
					  <!--<tr>-->
                         <!---->
				            <!--<td>-->
				            	<!--<input  class="form-control" placeholder="规格选项"> -->
				            <!--</td>-->
				            <!--<td>-->
				            	<!--<input  class="form-control" placeholder="排序"> -->
				            <!--</td>-->
							<!--<td>-->
								<!--<button type="button" class="btn btn-default" title="删除" ><i class="fa fa-trash-o"></i> 删除</button>-->
							<!--</td>-->
                      <!--</tr>-->
					  <!--<tr>-->
                           <!---->
				            <!--<td>-->
				            	<!--<input  class="form-control" placeholder="规格选项"> -->
				            <!--</td>-->
				            <!--<td>-->
				            	<!--<input  class="form-control" placeholder="排序"> -->
				            <!--</td>-->
							<!--<td>-->
								<!--<button type="button" class="btn btn-default" title="删除" ><i class="fa fa-trash-o"></i> 删除</button>-->
							<!--</td>-->
                      <!--</tr>-->
                    </tbody>
			  </table> 
			
			
		</div>
		<div class="modal-footer">						
			<button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="updateSpecification();">更新</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>
  </form>
  <!-- 新增窗口 -->
  <form id="addForm" action="#" method="post">
  <div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	  <div class="modal-dialog" >
		  <div class="modal-content">
			  <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				  <h3 id="myModalLabel1">规格新增</h3>
			  </div>
			  <div class="modal-body">

				  <table class="table table-bordered table-striped"  width="800px">
					  <tr>
						  <td>规格名称</td>
						  <td><input  class="form-control" placeholder="规格名称" name="spec_name">  </td>
					  </tr>
				  </table>

				  <!-- 规格选项 -->
				  <div class="btn-group">
					  <button type="button" class="btn btn-default" onclick="addOptions_add();" title="新建" ><i class="fa fa-file-o"></i> 新增规格选项</button>

				  </div>

				  <table class="table table-bordered table-striped table-hover dataTable">
					  <thead>
					  <tr>


						  <th class="sorting">规格选项</th>
						  <th class="sorting">排序</th>
						  <th class="sorting">操作</th>
					  </thead>
					  <tbody id="tbody_options_add">
					  <tr>
						  <td>
							  <input  class="form-control" name="option_name" placeholder="规格选项">
						  </td>
						  <td>
							  <input  class="form-control" name="orders" placeholder="排序">
						  </td>
						  <td>
							  <button type="button" class="btn btn-default" title="删除" onclick="deleteOption_add(this);"><i class="fa fa-trash-o"></i> 删除</button>
						  </td>
					  </tr>
					  <tr>

						  <td>
							  <input  class="form-control" name="option_name" placeholder="规格选项">
						  </td>
						  <td>
							  <input  class="form-control" name="orders" placeholder="排序">
						  </td>
						  <td>
							  <button type="button" class="btn btn-default" title="删除" onclick="deleteOption_add(this);"><i class="fa fa-trash-o"></i> 删除</button>
						  </td>
					  </tr>
					  <tr>

						  <td>
							  <input  class="form-control" name="option_name" placeholder="规格选项">
						  </td>
						  <td>
							  <input  class="form-control" name="orders" placeholder="排序">
						  </td>
						  <td>
							  <button type="button" class="btn btn-default" title="删除" onclick="deleteOption_add(this);"><i class="fa fa-trash-o"></i> 删除</button>
						  </td>
					  </tr>
					  </tbody>
				  </table>


			  </div>
			  <div class="modal-footer">
				  <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="addSpecification();">新增</button>
				  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			  </div>
		  </div>
	  </div>
  </div>
  </form>
</body>

</html>